//
//  ZYAssignmentController.m
//  HTW
//
//  Created by Ethan on 2017/9/22.
//  Copyright © 2017年 乾坤. All rights reserved.
//

#import "ZYAssignmentController.h"
#import "ZYAssignmentCell.h"
#import "IWTitleButton.h"
#define kCell @"cell"
#define KIWTitleButtonDownTag 0
#define KIWTitleButtonUpTag -1
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#import "UIImage+ZQK.h"
#import "ZYPopView.h"

@interface ZYAssignmentController ()<UITableViewDataSource,UITableViewDelegate,selsectIndexDelegate>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) ZYPopView *popView;
@property (nonatomic,strong) IWTitleButton *titleButton;
@end

@implementation ZYAssignmentController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUpSuperView];
    
}

- (void)setUpSuperView{
    
    
    //中间按钮
    IWTitleButton *titleButton = [IWTitleButton titleButton];
    self.titleButton = titleButton;
    //图标
    [titleButton setImage:[UIImage imageWithName:@"list_icon_list_selected"] forState:(UIControlStateNormal)];
    //文字
    [titleButton setTitle:@"张宇" forState:(UIControlStateNormal)];
    //位置和尺寸
    titleButton.frame = CGRectMake(0, 0, 100, 40);
    //添加事件
    [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.titleView = titleButton;
    //给个tag值
    titleButton.tag = KIWTitleButtonDownTag;
    
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.tableview.dataSource = self;
    self.tableview.delegate =self;
    [self.view addSubview:self.tableview];
    self.tableview.estimatedRowHeight = 200;
    self.tableview.tableFooterView = [UIView new];
    [self.tableview registerClass:[ZYAssignmentCell  class] forCellReuseIdentifier:kCell];
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYAssignmentCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self keyDisMiss];
    
    
}








- (void)titleClick:(IWTitleButton *)titleButton{
    //第一种方法
    
//    if (titleButton.tag == KIWTitleButtonUpTag) {
//        [titleButton setImage:[UIImage imageWithName:@"list_icon_list_selected"] forState:(UIControlStateNormal)];
//        titleButton.tag = KIWTitleButtonDownTag;
//    }else{
//        [titleButton setImage:[UIImage imageWithName:@"list_icon_list_selected"] forState:(UIControlStateNormal)];
//        titleButton.tag = KIWTitleButtonUpTag;
//    }

    
    if (self.popView) {
        [self.popView dismiss];
        self.popView  = nil;
    }
       CGPoint point = CGPointMake(kScreenWidth/2-40,64);
    self.popView = [[ZYPopView alloc] initFrameWithOrigin:point Width:80 Height:120 Type:XTTypeOfUpCenter Color:[UIColor blackColor]];
    self.popView.layer.masksToBounds = YES;
    self.popView.layer.cornerRadius = 5;
    self.popView.dataArray = @[@"只看男",@"只看女",@"看预约"];
    self.popView.fontSize = 13;
    self.popView.row_height = 40;
    self.popView.titleTextColor = [UIColor whiteColor];
    self.popView.delegate = self;
    [self.popView popView:self.view];
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self keyDisMiss];
    
}

- (void)keyDisMiss{
  
    [self.popView removeFromSuperview];
    self.popView = nil;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self keyDisMiss];
    
}
-(void)selectIndexPathRow:(NSInteger)index{
    NSLog(@"%ld",index);
    [self keyDisMiss];
}
@end
